package cn.yj.wx.pay.config;

import cn.yj.common.exception.PayException;
import cn.yj.commons.utils.FileUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
 * @author 永健
 * @since 2021-09-22 18:57
 */
@ConfigurationProperties(prefix = "pay.wechat")
public class WxPayConfig extends PayConfig {
    private String appId;
    private String mchSerialNo;
    private String mchId;
    private String apiV3Key;
    private String keyPath;
    private String notifyUrl;
    private String refundUrl;

    @Override
    public String getAppId() {
        return appId;
    }

    @Override
    public String getMchSerialNo() {
        return mchSerialNo;
    }

    @Override
    public String getMchId() {
        return mchId;
    }

    @Override
    public String getApiV3Key() {
        return apiV3Key;
    }


    public String getKeyPath() {
        return keyPath;
    }


    @Override
    public String getNotifyUrl() {
        return notifyUrl;
    }

    public WxPayConfig setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
        return this;
    }

    @Override
    public InputStream getKetStream() throws IOException {
        String fileContent = "";
        try {
            fileContent = FileUtils.getFileContent(keyPath, this.getClass());
            return new ByteArrayInputStream(fileContent.getBytes(StandardCharsets.UTF_8));
        } catch (UnsupportedEncodingException e) {
            throw new PayException(String.format("读取私钥失败:%s", fileContent));
        }
    }


    public WxPayConfig setAppId(String appId) {
        this.appId = appId;
        return this;
    }

    public WxPayConfig setMchSerialNo(String mchSerialNo) {
        this.mchSerialNo = mchSerialNo;
        return this;
    }

    public WxPayConfig setMchId(String mchId) {
        this.mchId = mchId;
        return this;
    }

    @Override
    public String getRefundUrl() {
        return refundUrl;
    }

    public WxPayConfig setRefundUrl(String refundUrl) {
        this.refundUrl = refundUrl;
        return this;
    }

    public WxPayConfig setApiV3Key(String apiV3Key) {
        this.apiV3Key = apiV3Key;
        return this;
    }

    public WxPayConfig setKeyPath(String keyPath) {
        this.keyPath = keyPath;
        return this;
    }
}
